Skip to content

💥 Subscribe after commit, and say whether there is a value - #291

Merged
mbret merged 3 commits into
mainfrom
claude/awesome-bell-2a040j
Nov 14, 2026
Merged

mbret merged 3 commits into
mainfrom
claude/awesome-bell-2a040j

Conversation

@mbret

@mbret mbret commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Why

A render that never commits left its useObserve subscription open for good, and started the side effects of the source. A Suspense boundary that suspends, an error boundary, an abandoned transition, a hidden <Activity> that unmounts and every server render make such renders. Through usePersistSignals, such a render kept saving the signals after unmount.

status also repeated observableState, so nothing told whether data was a value or the default. A BehaviorSubject was "pending" for its whole life.

  • Change: the hook subscribes in an effect, after commit, and status says whether the source has a value, as the queries of TanStack Query do.
  • Cost: a source that holds no value is "pending" on the first render and after a deps change, and that render can reach the screen.

What's new

Other changes

  • ♻️ usePersistSignals uses useObserve again, so it reads and writes storage after mount only
  • ✅ Lifecycle and server tests, and one test for each closed issue
  • 📝 Document the new first render, status and the stable source rule
  • 📝 AGENTS.md: choose the clean fix over the one that fits a patch

What breaks, and how to migrate

  • status: "pending" means that the source has no value yet. It meant a live stream: read observableState === "live" for that.
  • status: "success" means that the source has a value. It meant a completed stream: read observableState === "complete" for that.
  • Without a source, status is "pending", and observableState stays "complete".
  • On the first render, and after a deps change, a source that is not a BehaviorSubject or a signal is "pending", with defaultValue. A source that emits on subscription has its value in a second render, after mount. To read a value on the first render, keep it in a signal or a BehaviorSubject, or select part of a signal with useSignalValue(signal, selector).
  • An observable created during render without a function, such as source$.pipe(...) written in the component, makes React throw "Too many re-renders" once the component renders again. Create it in a function with deps.
  • usePersistSignals reads and writes storage after mount, not during render. With no entries, isHydrated is false on the first render, then true.

Details

The four closed issues

A BehaviorSubject that errored, or is closed, now reports its error on the first render. A closed one threw from subscribe, and #267 covered both.

Options rejected

  • A render subscription released by a 10 s timer, the first version of this PR. It changed no behaviour, so it fit a patch. But side effects still started during render and on the server, a render that never commits kept its subscription for 10 s, and a commit later than 10 s ran the source twice.
  • Subscribe and unsubscribe at once during render. The value is there on the first render, and nothing leaks. But a cold source runs twice, once in render and once after commit, and its side effects start during render.
  • Keep the observation in useMemo. It shows the same values. But React may drop a memo (Fast Refresh does), and a source created on every render loops without an error: each render makes a new memo, and each commit subscribes again. That loop runs in passive effects, which React does not stop, and in production nothing reports it. In state, the same source makes React throw "Too many re-renders".
  • Subscribe in a layout effect. A value emitted on subscription would replace the pending state before the browser paints. But React cleans up the layout effects of content that a Suspense boundary hides when it suspends again. A cold source then ran again, and usePersistSignals hydrated over a signal update made meanwhile.

One observation per deps change

Each deps change creates one observation: the source, the deps, and the state render shows before the effect subscribes. The hook keeps it in state, with the latest state of its source. When the deps change, render replaces both with setState, and React renders again at once. This is the React pattern to store information from previous renders. A render that React throws away discards that setState too. The state belongs to the observation, so:

  • a deps change always subscribes again, also to the same observable;
  • a source that completed, then another source, then the first one again, subscribes again instead of staying "pending";
  • an effect that React runs again for the same observation (StrictMode, <Activity>) does not subscribe again to a source that ended.

Tests seen to fail

  • On main: every lifecycle and server test of a render that never commits, and the test of each closed issue. For example, useObserve: a transition and an urgent render make the mounted component run its source again #290 subscribes the committed source twice, and useObserve: throws during render for a BehaviorSubject that errored or is closed #267 throws boom.
  • On the first version of the redesign, the tests of the review findings fail: the source that comes back, the same observable under new deps, the closed subject, a Suspense boundary that suspends again, and the persistence update made meanwhile.
  • Without the setState during render, the test of a source created on every render runs until the test worker runs out of memory and aborts.
  • I removed each part of the new hook in turn: what an observation remembers across effects, the reset of the state on a new observation, the equality check of a new value, and the memo of the result. Each removal fails a test. Widening data in the "success" type fails the type check.

Performance

I measured a mount of 1,000 components in jsdom, on main and on this branch. "Renders" counts the calls of the component function.

Source Renders on main Renders on this branch Time on main Time on this branch
BehaviorSubject 1,000 1,000 47 ms 27 ms
A piped signal 1,000 2,000 50 ms 50 ms
An async Subject 1,000 1,000 62 ms 37 ms
  • A BehaviorSubject or a signal renders once. Its value on subscription is equal to the value render read, so it sets no state.
  • A source that emits on subscription and holds no value, such as a piped signal, renders twice on mount. useSignalValue(signal, selector) renders it once.
  • A deps change calls the component function twice. React discards the first call before it renders the children, and commits once.
  • Nothing subscribes for a render that React throws away, or on the server.

Left for other issues

Release notes

The BREAKING CHANGE: footer of the useObserve commit makes this reactjrx 6, and lists the migration steps above.

🔍 Self-reviewed with selfcorrect-review
🤖 Made within harness coverage

🤖 Generated with Claude Code

https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm

@mbret
mbret marked this pull request as ready for review November 13, 2026 00:19
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T18:27:37.395487Z 098709a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@mbret
mbret marked this pull request as draft November 13, 2026 00:50
A design is never chosen for the release type it gets. When a problem
has a clean fix that breaks callers and a weaker one that ships as a
patch, the clean fix wins and ships as a major version, and the pull
request names the options it rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm
…value

useObserve subscribed to its source during render, so that a source
that emits synchronously had its value on the first render. A render
that React throws away, and every server render, left that subscription
open, and started the side effects of the source.

The hook now subscribes in an effect, after React commits. Render reads
only what a source holds without a subscription: the value of a
BehaviorSubject or a signal.

Each deps change starts a new observation, kept in state. Render
replaces the observation and the state of its source together, so a
source created on every render makes React throw "Too many re-renders"
instead of subscribing again without end.

status now says whether the source has a value, as the queries of
TanStack Query do, and data is typed by it. observableState keeps the
life of the stream.

A BehaviorSubject that errored or is closed no longer throws during
render, a new defaultValue applies while the source has no value, and
the result keeps its identity across renders that change nothing.

usePersistSignals uses useObserve again, so it reads and writes storage
after mount only.

Closes #261
Closes #267
Closes #279
Closes #280
Closes #290

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm

BREAKING CHANGE: `useObserve` subscribes after commit, in an effect, and `status` says whether the source has a value.

- `status: "pending"` means that the source has no value. It meant a live stream: read `observableState === "live"` for that.
- `status: "success"` means that the source has a value. It meant a completed stream: read `observableState === "complete"` for that.
- Without a source, `status` is `"pending"`, and `observableState` stays `"complete"`.
- On the first render, and after a deps change, a source that is not a `BehaviorSubject` or a signal is `"pending"`, with `defaultValue`. A source that emits on subscription has its value in a second render, after mount. To read a value on the first render, keep it in a signal or a `BehaviorSubject`, or select part of a signal with `useSignalValue(signal, selector)`.
- An observable created during render without a function, such as `source$.pipe(...)` written in the component, makes React throw "Too many re-renders". Create it in a function with `deps`.
- `usePersistSignals` reads and writes storage after mount. With no entries, it returns `isHydrated: false` on the first render, then `true`.
@mbret
mbret force-pushed the claude/awesome-bell-2a040j branch from 2c0ebec to 098709a Compare November 13, 2026 18:21
@mbret mbret changed the title 🐛 Release the subscription of a render that never commits 💥 Subscribe after commit, and say whether there is a value Nov 13, 2026
@mbret
mbret marked this pull request as ready for review November 13, 2026 18:24

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 098709aea2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The previous commit called compareFn in the next handler of the
subscription. RxJS reports a throw there as unhandled, so the hook
stayed live with stale data. The comparison now runs in a filter
operator, and a throw errors the stream, as distinctUntilChanged did
before: status is "error", data keeps the last value, and the hook
unsubscribes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm
@mbret
mbret merged commit 8df2eee into main Nov 14, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants